home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / Matlab mode / matlabWindows.tcl < prev    next >
Encoding:
Text File  |  2000-06-05  |  6.9 KB  |  238 lines

  1. ################################################################################
  2. #
  3. # matlabWindows.tcl, part of the matlab mode package
  4. # Command and history windows
  5. ################################################################################
  6.  
  7. dummyMATL
  8. proc matlabWindows.tcl {} {}
  9.  
  10. ################################################################################
  11. #  Create and/or go to command window
  12. ################################################################################
  13.  
  14. proc matlabCmdWindow {} {
  15.     global winModes MATLmodeVars
  16.  
  17.     set wins [winNames]
  18.     if {[set winThere [lsearch $wins $MATLmodeVars(CmdWinName)]] >= 0} {
  19.         set name [lindex $wins $winThere]
  20.         bringToFront $name
  21.         goto [maxPos]
  22.     } else {
  23.         new -n $MATLmodeVars(CmdWinName) -m MATL
  24.         setWinInfo {shell} {1}
  25.         insertText "Welcome to Alpha's Matlab shell."
  26.         insertText -w [lindex [winNames] 0] [matlabPrompt]
  27.     }
  28. }
  29.  
  30.  
  31. ################################################################################
  32. #  Write results to command window
  33. ################################################################################
  34.  
  35. proc matlabResults {res} {
  36.     global lastMatlabResult
  37.     
  38.     matlabCmdWindow
  39.     insertText "${res}[matlabPrompt 0]"
  40.     set lastMatlabResult $res
  41. }
  42.  
  43.  
  44. ################################################################################
  45. #  Check if front window is the command window
  46. ################################################################################
  47.  
  48. proc matlabIsShell {} {    
  49.     global MATLmodeVars
  50.     return [expr [string compare [lindex [winNames] 0] $MATLmodeVars(CmdWinName)] == 0]
  51. }
  52.  
  53.  
  54. ################################################################################
  55. #  Check if front window is the command history window
  56. ################################################################################
  57.  
  58. proc matlabIsHist {} {    
  59.     global MATLmodeVars
  60.     return [expr [string compare [lindex [winNames] 0] $MATLmodeVars(CmdHistWinName)] == 0]
  61. }
  62.  
  63.  
  64. ################################################################################
  65. #  Send shell line to MATLAB
  66. ################################################################################
  67.  
  68. proc matlabDoShellLine {} {
  69.     
  70.     set pos [getPos]
  71.     
  72.     set ind [string first "»" [getText [lineStart $pos] [nextLineStart [getPos]]]]
  73.     if {$ind >= 0} {
  74.     set lStart [pos::math [lineStart $pos] +$ind+2]
  75.     endOfLine
  76.     set scriptName [getText $lStart [getPos]]
  77.     if {[pos::compare [getPos] != [maxPos]]} {
  78.         goto [maxPos]
  79.         insertText $scriptName
  80.     }
  81.     
  82.     insertText "\n"
  83.     matlabDoScript $scriptName 2
  84.     matlabAddCommandHistory $scriptName
  85.     
  86.     } else {
  87.     
  88.     # If we're not on a command line, either 
  89.     # 1) insert a command prompt (if at the bottom of the window), or
  90.     # 2) insert an ordinary return
  91.         
  92.     if {[pos::compare [getPos] == [maxPos]]} {
  93.         insertText [matlabPrompt]
  94.     } else {
  95.         insertText "\r"
  96.     }
  97.     }
  98.     return
  99. }
  100.  
  101.  
  102. ################################################################################
  103. #  Command History recall
  104. ################################################################################
  105.  
  106. proc matlabPrevCommand {} {
  107.     global commandHistory commandNum
  108.     
  109.     set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
  110.     if {[set ind [string first "» " $text]] == 0} {
  111.     goto [pos::math [lineStart [getPos]] + $ind + 2]
  112.     } else return
  113.     
  114.     incr commandNum -1
  115.     if {$commandNum < 0} {
  116.     incr commandNum
  117.     endOfLine
  118.     return
  119.     }
  120.     set text [lindex $commandHistory $commandNum]
  121.     set to [nextLineStart [getPos]]
  122.     if {[is::Eol [lookAt [pos::math $to -1]]]} {set to [pos::math $to -1]}
  123.     replaceText [getPos] $to $text
  124. }
  125.  
  126.  
  127. proc matlabNextCommand {} {
  128.     global commandHistory commandNum
  129.     
  130.     set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
  131.     if {[set ind [string first "» " $text]] == 0} {
  132.     goto [pos::math [lineStart [getPos]] + $ind + 2]
  133.     } else return
  134.     
  135.     incr commandNum
  136.     if {$commandNum >= [llength $commandHistory]} {
  137.     incr commandNum -1
  138.     matlabCancelLine
  139.     return
  140.     }
  141.     set text [lindex $commandHistory $commandNum]
  142.     set to [nextLineStart [getPos]]
  143.     if {[is::Eol [lookAt [pos::math $to -1]]]} {set to [pos::math $to -1]}
  144.     replaceText [getPos] $to $text
  145. }
  146.  
  147.  
  148. ################################################################################
  149. #  Clear current line
  150. ################################################################################
  151.  
  152. proc matlabCancelLine {} {
  153.     global commandHistory commandNum
  154.     
  155.     if {![matlabIsShell]} {return}
  156.     
  157.     set text [getText [lineStart [getPos]] [nextLineStart [getPos]]]
  158.     if {[set ind [string first "» " $text]] == 0} {
  159.     goto [pos::math [lineStart [getPos]] + $ind + 2]
  160.     } else return
  161.     
  162.     set to [nextLineStart [getPos]]
  163.     deleteText [getPos] $to
  164.     
  165.     set commandNum [llength $commandHistory]
  166. }
  167.  
  168.  
  169. ################################################################################
  170. #  Prompt, for command window
  171. ################################################################################
  172.  
  173. proc matlabPrompt {{cr 1}} {
  174.     if {$cr} {
  175.     return "\r» "
  176.     } else {
  177.     return "» "
  178.     }
  179. }
  180.  
  181. ################################################################################
  182. #  Create and/or go to the command history window
  183. ################################################################################
  184.  
  185. proc matlabCmdHistWindow {} {
  186.     global winModes MATLmodeVars commandHistory
  187.  
  188.     set wins [winNames]
  189.     if {[set winThere [lsearch $wins $MATLmodeVars(CmdHistWinName)]] >= 0} {
  190.         set name [lindex $wins $winThere]
  191.         bringToFront $name
  192.     } else {
  193.         new -n $MATLmodeVars(CmdHistWinName) -m MATL
  194.         set text "(<cr> to rexecute the command)\r"
  195.         set theTime [join [mtime [now] long]]
  196.         append text "---- Command session history beginning $theTime ----\r"
  197.         set numLines [llength $commandHistory]
  198.         for {set commandNum 0} {$commandNum < $numLines} {incr commandNum 1} {
  199.             append text "[lindex $commandHistory $commandNum]\r"
  200.         }
  201.         insertText $text
  202.         setWinInfo {dirty} {0}
  203.         setWinInfo {read-only} {1}
  204.         prevLineSelect
  205.     }
  206. }
  207.  
  208.  
  209. ################################################################################
  210. #  Add a command to the history list and the command history window if open
  211. ################################################################################
  212.  
  213. proc matlabAddCommandHistory {scriptName} {
  214.     global commandHistory commandNum MATLmodeVars
  215.  
  216.     if {[string compare [lindex $commandHistory [expr [llength $commandHistory]-1]] $scriptName] != 0} {
  217.         lappend commandHistory $scriptName
  218.         
  219.         if {[set winThere [lsearch [winNames] $MATLmodeVars(CmdHistWinName)]] >= 0} {
  220.             set currentWindow [lindex [winNames] 0]
  221.             bringToFront $MATLmodeVars(CmdHistWinName)
  222.             goto [maxPos -w $MATLmodeVars(CmdHistWinName)]
  223.             setWinInfo -w $MATLmodeVars(CmdHistWinName) {read-only} {0}
  224.             insertText -w $MATLmodeVars(CmdHistWinName) "$scriptName\r"
  225.             setWinInfo -w $MATLmodeVars(CmdHistWinName) {dirty} {0}
  226.             setWinInfo -w $MATLmodeVars(CmdHistWinName) {read-only} {1}
  227.             prevLineSelect 
  228.             bringToFront $currentWindow
  229.         }
  230.     }
  231.     
  232.     set commandNum [llength $commandHistory]
  233. }
  234.  
  235.  
  236.